*L14.3.6.2	"Listing of Abnormal On-Study ECG Assessments" (Safety Population);
*L14.3.6.3	"Listing of Clinically Significant On-Study ECG Assessments" (Safety Population);


%macro print_listings(bbb, program, idisname, disname, ds_list, ttt, fr, ftnt);
%get_data(lst=adeg,pre=adam,supp=N,lib=adam);

proc sql;
select count(distinct usubjid)
into :doit
from adamw.adeg
	where	index(avalc,&bbb)>0 and saffl="Y";
quit;



proc sql;

create table egintp as select usubjid, subjid, avisit, avisitn, visityp, adt, atm,egpos, /*atpt, atptn,*/ strip(upcase(avalc)) as safety
	from adamw.adeg where saffl="Y" and paramcd="INTP" and index(avalc,&bbb)>0 and avisit ne "Screening" 
    /* and index(visityp,"Scheduled")>0*/ 
			order by usubjid, avisitn,/* atptn,*/ adt, atm;
create table egrest as select usubjid, subjid, avisit, avisitn, visityp, paramcd, param, adt, atm,egpos, /*atpt, atptn,*/ avalc
	from adamw.adeg where saffl="Y" and paramcd ne "INTP" and avisit ne "Screening" 
/*		and avisit ne "Post Study Day 21/Early Termination" and index(visityp,"Scheduled")>0 */
			order by usubjid, avisitn, /*atptn,*/ adt, atm;
quit;

data eg1;
length adtc $10. atmm $5.;
merge egintp(in=aa) egrest;
by usubjid avisitn adt atm;
if aa;
avalc=upcase(avalc);
*safety=tranwrd(safety,"ABNORMAL, NOT CLINICALLY SIGNIFICANT","ABNORMAL NCS");
*safety=tranwrd(safety,"ABNORMAL, CLINICALLY SIGNIFICANT","ABNORMAL CS");
if atm ne . then do;
	atmm=strip(put(atm,time5.));
	if length(atmm)=4 or strip(substr(atmm,2,1))=":" then atmm="0"||strip(atmm);
		else atmm=atmm;
end;
adtc=strip(put(adt,??is8601da.));
dt=strip(adtc)||"/"||strip(atmm);
*avisit=tranwrd(avisit,"Post Study Day 21/Early Termination","Post Study Day 21");
if index(param,"PR Interval") then od=2;
else if index(param,"QTcF Interval") then od=6;
else if index(param,"QTcB Interval") then od=6;
else if index(param,"QT Interval") then od=4;
else if index(param,"QRS Duration") then od=3;
else if index(param,"ECG Mean Ventricular Rate") then od=1;
else od=7;
run;


*Obtain EGPOS from SDTM;

/*data infor;*/
/*merge eg1(in=bb) egpos;*/
/*by usubjid avisitn;*/
/*if bb;*/
/*if avisit not in ("Screening Day -28 to -1" "Post Study Day 21/Early Termination");*/
/*run; */

proc sort data=eg1 out=infor; by usubjid subjid paramcd avisitn adt atm paramcd; run;

%page_cut(in=infor,sort=subjid avisitn adt atm od paramcd,nocut=avisitn ,newpage=,pagesize=24,skip=,order=,columns=,
		width=,defln=1,nlsym=\par,out=report);

/*data report;*/
/*set infor;*/
/*by usubjid subjid avisitn atptn adt atm paramcd;*/
/*retain nb page 0;*/
/*if first.avisitn and nb GE 24 then do;*/
/*page+1;*/
/*nb=1;*/
/*end;*/
/*else if first.avisitn then nb+2;*/
/*else nb+1;*/
/*run;*/


%empty_ds_fix(report);

filename filetmp temp;
title3 " ";
title4 "Table &disname (Continued)";
filename filertf "\\algopharm.com\algorithmepharmadata\Biostudies\Montreal\&prot.\SRA\Biostatistics\Work\Outputs\tables\T&idisname..EG.ABN.&fr.rtf";

%footer1(foot1=1, ds_list=&ds_list.,program=&program.);

option orientation=landscape missing=" ";
%open_rtf;
ods listing close;
ods escapechar="^";
ods rtf file=filetmp style=styles.Algo1;
title5 "Listing of &ttt On-Study ECG Assessments";
title6 "(Safety Population)";

footnote3 j=l "[1] &ftnt";
/*Subject ID	Visit		Date / Time		Position	Safety Review [1]	Parameter (Unit)	Value	*/
proc report data=report nowd split='~' missing;
column page  USUBJID SUBJID avisitn avisit dt egpos safety od param avalc ;
define page        / " " order order=internal noprint;
define usubjid      / " " order order=internal noprint;
define subjid     / "Subject ID~ " order order=internal style(column)=[cellwidth=1.0in just=left] style(header)=[just=left];
define avisitn     / " " order order=internal noprint;
define avisit      / "Visit~ " order order=internal style(column)=[cellwidth=1.3in just=left] style(header)=[just=left];
*define atptn     / " " order order=internal noprint;

*define atpt     / "Time Point~ " order order=internal style(column)=[cellwidth=0.9in just=left] style(header)=[just=left];
define dt        / "Date/Time~ " order order=internal style(column)=[cellwidth=1.3in just=left] style(header)=[just=left];
/*define atmm        / "Time" order order=internal style(column)=[cellwidth=0.5in just=left] style(header)=[just=left];*/
define egpos       / "Position~ " order order=internal style(column)=[cellwidth=1in just=left] style(header)=[just=left];
define safety      / "MD Safety Review~[1]" order order=internal style(column)=[cellwidth=1.3in just=left] style(header)=[just=left];
define od     / " " order order=internal noprint;
define param       / "Parameter (Unit)~ " style(column)=[cellwidth=3.0in just=left] style(header)=[just=left];
define avalc       / "Value~ " style(column)=[cellwidth=0.9in just=left] style(header)=[just=left];
 
 break after page   / page;

 compute before subjid;
	 line " ";
 endcomp;

  compute after avisitn;
	 line " ";
 endcomp;

%if &doit EQ 0 %then %do;
 	compute after page;
	line "No &ttt On-Study ECG Assessments Were Measured.";
endcomp;
%end;
run;

%close_rtf;
%arrange_rtf;

%mend print_listings;

%print_listings(bbb=%str("NCS"), program=T14_03_06_0X_ABN_EG, idisname=14.3.6.2, disname=14.3.6.2, ds_list=ADEG, ttt=Abnormal, fr=ALL., ftnt=%str(NCS: Not Clinically Significant / CS: Clinically Significant));

proc datasets lib=work kill nolist nodetails; run;

%print_listings(bbb=%str("/*Abnormal, */CS"), program=T14_03_06_0X_ABN_EG, idisname=14.3.6.3, disname=14.3.6.3,  ds_list=ADEG, ttt=Clinically Significant, fr=CS., ftnt=%str(CS: Clinically Significant));
proc datasets lib=work kill nolist nodetails; run;
